package com.tql.work.fari;

import com.rabbitmq.client.*;
import com.tql.util.ConnectionUtil;

import java.io.IOException;

public class Consumer1 {
    private final static String QUEUE_NAME="first_queue";
    public static void main(String[] args) throws Exception{
        Connection connection=null;
        try{
            //获取连接对象
            connection= ConnectionUtil.getConnection();
            //创建频道
            final Channel channel = connection.createChannel();

         /*    * @param1 队列名称
             * @param2 是否持久化
             * @param3 是否支持排他性
             * @param4 最后一个消费者消费完消息是否自动删除，
             * @parma5 传入参数
             * */
            //获取频道参数
            //定义指标
            channel.basicQos(1);
            System.out.println("________________");
           channel.basicConsume("queue1", false, new DeliverCallback() {
               public void handle(String s, Delivery delivery) throws IOException {
                   System.out.println("接收到的消息为"+new String(delivery.getBody(),"utf-8"));
                   System.out.println("+++++++++++++++++++++++++++");
                   try {
                       Thread.sleep(1000);
                   } catch (InterruptedException e) {
                       e.printStackTrace();
                   }
                   channel.basicAck(delivery.getEnvelope().getDeliveryTag(),false);
               }
           }, new CancelCallback() {
               public void handle(String s) throws IOException {
                   System.out.println("接收信息失败");
               }
           });
            System.out.println(QUEUE_NAME+"开始接收消息");
            System.in.read();
        }catch (Exception e){
            e.printStackTrace();
        }
    }

}
